// pages/user/applyDistributor/applyDistributor.js
var app = getApp();
var user_id= '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    phonenum: '',
    idcard: '',
    recommender: '',
    reason: '',
    isdisbale: false,
    dealer_apply: false,
    isshow: false,
    account: '',
    reconame:'',//推荐人姓名
    recophone:'',//推荐人电话
    recocard:'',//推荐人证件
    referee_is_default:1,//是否是默认推荐人
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUserInfo();
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  // 申请
  formSubmit(e) {
    var that = this;
    var name = e.detail.value.name;
    var phonenum = e.detail.value.phonenum;
    var idcard = e.detail.value.idcard;
    var recommender = e.detail.value.recommender;
    var reason = e.detail.value.reason;
    that.setData({
      isdisbale: true
    })
    if (recommender == '') {
      recommender = that.data.account
    }
    app.ajax({
      url: 'User/Spread/dealerApply',
      method: "POST",
      data: {
        username: name,
        phone: phonenum,
        id_card: idcard,
        referee_code: recommender,
        reason: reason,
        user_id: user_id
      },
      success: function(res) {
        if (res.data.code == 1000) {
          app.showToast(res.data.message);
          that.setData({
            isdisbale: true
          })
          setTimeout(function() {
            wx.switchTab({
              url: '../userHome/userHome',
            })
          }, 2000)
        } else {
          that.setData({
            isdisbale: false
          })
          app.showToast(res.data.message);
        }
      }
    })
  },

  // 获取认证信息
  getAuthInfo() {
    var that = this;
    app.ajax({
      url: 'User/Realname/getAuthInfo',
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data.code == 1000) {
          that.setData({
            name: res.data.data.realname,
            idcard: res.data.data.id_number,
          })
        }
      }
    })
  },

  // 获取用户信息
  getUserInfo() {
    var that = this;
    app.ajax({
      url: 'User/User/getUserInfo',
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data.code == 1000) {
          var is_auth = res.data.data.is_auth;
          that.setData({
            phonenum: res.data.data.telphone,
            referee_is_default: res.data.data.source_is_default
          })
          user_id= res.data.data.user_id;
          if (is_auth != 1) {
            that.getAuthInfo()
          }
          that.getApply();
        }
      }
    })
  },

  // 扫码
  sweepCode() {
    var that = this;
    wx.scanCode({
      success: (res) => {
        app.showToast('扫码成功');
        console.log(res)
        var code = JSON.parse(res.result)
        that.setData({
          recommender: code.code
        })
      },
      fail: (res) => {
        app.showToast('扫码失败，请重试！');
      }
    })
  },

  // 获取申请信息
  getApply() {
    var that = this;
    app.ajax({
      url: 'User/Spread/getApply',
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data.code == 1000) {
          var recommender = res.data.data.inviter_account;
          if (res.data.data.referee_is_default==2){
            recommender = '';
          }
          that.setData({
            referee_is_default: res.data.data.referee_is_default,
            recommender: recommender,
            reason: res.data.data.reason,
            reconame: res.data.data.inviter_realname,
            recophone: res.data.data.inviter_telphone,
            recocard: res.data.data.inviter_id_card,
            isshow: true,
            dealer_apply: true
          })
        } else {
          that.getInviter()
        }
      }
    })
  },
  
  getInviter() {
    var that = this;
    app.ajax({
      url: 'User/Spread/Inviter',
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data.code == 1000) {
          if (that.data.referee_is_default==1) {
            that.setData({
              recommender: res.data.data.account,
              reconame:res.data.data.name,
              recophone: res.data.data.telphone,
              recocard: res.data.data.id_card,
              isshow: true
            })
          } else {
            that.setData({
              recommender: ''
            })
          }
        }
      }
    })
  }

})